package it.infodea.tapestrydea.services.pageinfo.bean;

import it.infodea.tapestrydea.support.annotations.PageInfo;

public class PageInfoBean {

	final private PageInfo pageInfo;

	final private String category;

	final private String parentCategory;

	private String link;

	private String context;

	private String label;

	private PageInfoBean(String parentCategory, String category) {
		this.pageInfo = null;
		this.parentCategory = parentCategory;
		this.category = category;
	}

	public PageInfoBean(PageInfo pageInfo, String link, String label) {
		this.pageInfo = pageInfo;
		category = pageInfo.category();
		parentCategory = pageInfo.parentCategory();
		this.link = link;
		this.label = label;

	}

	public boolean isNormalMode() {
		return pageInfo != null && PageInfo.MODE_NORMAL.equals(pageInfo.mode());
	}

	public String getCategory() {
		return category;
	}

	public String getParentCategory() {
		return parentCategory;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public String getLabel() {
		return label;
	}

	public String getLink() {
		return link;
	}

	public String getPagePath() {
		String pagePath = link.substring(0, link.length() - label.length() - 1);
		return pagePath;
	}

	public static PageInfoBean newCategoryLink(String parentCategory, String category) {
		return new PageInfoBean(parentCategory, category);
	}

	public int getPageSecuritylevel() {
		return pageInfo.allowedSecurityGroup().getLevel();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((category == null) ? 0 : category.hashCode());
		result = prime * result + ((context == null) ? 0 : context.hashCode());
		result = prime * result + ((label == null) ? 0 : label.hashCode());
		result = prime * result + ((link == null) ? 0 : link.hashCode());
		result = prime * result
				+ ((pageInfo == null) ? 0 : pageInfo.hashCode());
		result = prime * result
				+ ((parentCategory == null) ? 0 : parentCategory.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {

		if (this == obj)					return true;
		
		if (obj == null)					return false;
		
		if (getClass() != obj.getClass())	return false;
		
		PageInfoBean other = (PageInfoBean) obj;
		if (category == null && other.category != null)	return false;
		else if (category != null && !category.equals(other.category))	return false;

		if (context == null && other.context != null)	return false;
		else if (context != null && !context.equals(other.context))		return false;

		if (label == null && other.label != null)		return false;
		else if (label != null && !label.equals(other.label))			return false;
		
		if (parentCategory == null && other.parentCategory != null)	return false;
		else if (parentCategory != null && !parentCategory.equals(other.parentCategory))		return false;

		
		return true;
	}
	
}
